var express = require('express');
// var sql = require('../databases/sql');
var router = module.exports = express.Router();
var mysql = require('mysql');
var connection = mysql.createConnection({host: 'localhost', database: 'aaa', user: 'root', password: 'root'});

var cookoeSession = require('cookie-session')({keys: ['fasd', 'dawd']});

router.use(cookoeSession);


router.get('/', function (req, res) {

    var data = req.query;
    console.log("名字" + data.name + "     密码" + data.psw);
   if(data==null){ res.render('index');}


   else {

       tt(data.name, data.psw, s, res);
    //     console.log('yes');
    //     res.redirect('main');
     }



})


function tt(name, psw, callback, res) {


    connection.query('SELECT * FROM  user1 WHERE name=?', [name, psw, res], function (err, result) {

        callback(result, psw, res);

    });


}


function s(data, psw, res) {


    if (data == null | data == "") {
        console.log('用户不存在');
        return;
    } else {

        console.log(data[0]);
        if (data[0].psw != psw) {

            console.log('密码错误');
            return;


        } else {
            console.log('登陆成功')
             res.redirect('main');

        }


    }


}


router.get('/main', function (req, res) {
    res.render('main');


})